<template>
  <div class="login">
    <!--登录表单-->
    <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
      <h1 class="title">
        <i class="el-icon-setting"></i>
        vuexms登录
      </h1>

      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      //登录表单的数据
      loginForm: {
        username: '',
        password: ''
      },
      //验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }, //非空
          { min: 5, max: 18, message: '长度在 5 到 18 个字符', trigger: 'blur' } //长度
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 18, message: '长度在 5 到 18 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //保留this
          let _this = this;
          // 收集用户名和密码,发送给后端
          //console.log(this.loginForm.username, this.loginForm.password,this.axios)
          this.axios.post('/api/checklogin',{
            username: _this.loginForm.username,
            password: _this.loginForm.password
          })
            .then(response => {
              if(response.data.length){
                console.log('接收后端响应登录请求的数据:',response.data[0])

                //把用户信息 存入本地存储
                // localStorage.setItem('userinfo', JSON.stringify(response.data[0]))

                // let userinfo = JSON.parse(localStorage.getItem('userinfo'))

                //把当前用户数据存入state
                _this.$store.commit('SAVE_USERINFO', response.data[0])
                // _this.$store.commit('SAVE_USERINFO', userinfo)

                _this.$message({
                  message: '恭喜你，登录成功',
                  type: 'success'
                });
                //跳转到后台首页
                _this.$router.push('/')
              }else {
                _this.$message.error('请检查用户名或密码');
              }
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="stylus">
html,body
  margin 0
  padding 0
html,body,#app,.login
  height 100%
  .login
    display flex
    justify-content center
    align-items center
    .el-form
      width 400px
      border 1px solid #ccc
      border-radius 4px
      padding 0 45px 10px 10px
      .title
        font-size 22px
        color #ccc
        font-weight bold
        padding 20px 0px 20px 60px
</style>
